#include "settings.h"
#include "ui_settings.h"
#include "mainwindow.h"
#include "audiooutputer.h"
#include <irrKlang/irrKlang.h>
#include "savemanager.h"

using namespace irrklang;
irrklang::ISoundEngine* engine2;
double vols = 50.0;
Settings::Settings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Settings)
{
    engine2 = irrklang::createIrrKlangDevice();
    ui->setupUi(this);
    SaveManager::GetInstance()->LoadConfigs();
    vols = SaveManager::GetInstance()->GetVolume();
    ui->horizontalSlider->setValue(int(vols));
    ui->txtPassword->setText(SaveManager::GetInstance()->GetPassword());
    ui->txtUsername->setText(SaveManager::GetInstance()->GetUserName());
    ui->chkSkype->setChecked(SaveManager::GetInstance()->GetSkypeBot());
}

Settings::~Settings()
{
    engine2->drop();
    delete ui;
}

void Settings::on_horizontalSlider_sliderMoved(int position)
{
    vols = double(position);
}

void Settings::on_buttonBox_accepted()
{
    AudioOutputer::GetInstance()->SetVolume(vols);
    SaveManager::GetInstance()->SetVolume(vols);
    SaveManager::GetInstance()->SetPassword(ui->txtPassword->text());
    SaveManager::GetInstance()->SetUserName(ui->txtUsername->text());
    SaveManager::GetInstance()->SetSkypeBot(ui->chkSkype->isChecked());
    SaveManager::GetInstance()->SaveConfigs();
}

void Settings::on_horizontalSlider_sliderReleased()
{
    engine2->setSoundVolume(vols/100);
    engine2->play2D("wav/test.wav");
}

void Settings::on_chkSkype_toggled(bool checked)
{
    ui->groupBox_2->setEnabled(checked);
}
